<?php

	class usuarios
	{
		private $sql = "";
		private $id;
		private $nome;
		private $cpf;
		private $matriculaRm;
		private $senha;
		private $setor;
		private $cargo;
		private $status;
		private $ultimoAcesso;
		private $matriculaOi;
		private $dataCadastro;
		private $telefone;
		private $email;
		private $idSuperior;
		private $nomeSuperior;
		
	 	public function __get($property) {
			if (property_exists($this, $property)) {
			  return $this->$property;
			}
		}
		
		public function __set($property, $value) {
			if (property_exists($this, $property)) {
			  $this->$property = $value;
			}		
		    return $this;
		}
		
		function usuarios(){			
			include('../includes/conn.php');
			$idSuperior = 1;					
		}
		
		public function CadastrarUsuario()
		{
			include('../includes/conn.php');
			$sql = "";
			$sql = "INSERT INTO usuarios SET 
							nome = '".$this->nome."'
							,cpf = ".$this->cpf."							
							,senha = '".$this->senha."'
							,setor = '".$this->setor."'
							,cargo = '".$this->cargo."'
							,status = '".$this->status."'							
							,dataCadastro = now()
							,telefone = '".$this->telefone."'
							,email = '".$this->email."'";
							if($this->idSuperior != null)
							{													
								$sql .= ",idSuperior = ".$this->idSuperior;														
							}
			
			try
			{
				$db->query($sql);
				return "Cadastrado OK";		
			}
			catch(Exception $e)
			{
				echo "Exceção pega: ",  $e->getMessage(), "\n";
			}
		}
		
		public function AlterarUsuario()
		{
			include('../includes/conn.php');
			$sql = "";
			$sql = "UPDATE usuarios 
						SET 
							nome = '".$this->nome."',
							cpf = ".$this->cpf.",
							senha = '".$this->senha."',
							setor = '".$this->setor."',
							cargo = '".$this->cargo."',
							status = '".$this->status."',							
							telefone = '".$this->telefone."',
							email = '".$this->email."'";
							if($this->idSuperior != null)
							{													
								$sql .= ",idSuperior = ".$this->idSuperior;														
							}
							$sql .= " WHERE id = ".$this->id ;	

			try
			{
				$db->query($sql);
				return "Alterado OK";		
				//return $sql;
			}
			catch(Exception $e)
			{
				echo "Exceção pega: ",  $e->getMessage(), "\n";
			}		
		}
		
		public function BuscaUsuario($id)
		{
			include('../includes/conn.php');
			$sql = "";
			$sql = "SELECT * FROM usuarios WHERE id = ".$id ;							
			$ret = $db->query($sql);
			
			if($ret)
			{
				
				$id = $ret ['id'];
				$nome = $ret['nome'];
				$cpf = $ret['cpf'];
				$senha = $ret['senha'];
				$setor = $ret['setor'];
				$cargo = $ret['cargo'];
				$status = $ret['status'];
				$ultimoAcesso = $ret['ultimoAcesso'];			
				$dataCadastro = $ret['dataCadastro'];
				$telefone = $ret['telefone'];
				$email = $ret['email'];
				$idSuperior = $ret['idSupervisor'];
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		
	}
?>